import { Component, OnInit } from "@angular/core";
import {
  FormBuilder,
  FormGroup,
  Validators,
  FormControl
} from "@angular/forms";
import { Router } from "@angular/router";
import { LoginBean } from "./bean/login.bean";
import {
  NzNotificationService,
  NzModalSubject,
  NzModalService
} from "ng-zorro-antd";
import { HttpServcie } from "../../service/http.service";
import { GlobalVars } from "../../service/globalVars";
import { Config } from "../../service/config";

@Component({
  selector: "app-login",
  templateUrl: "./login.html",
  styleUrls: ["./login.scss"]
})
export class Login implements OnInit {
  loginBean: LoginBean = new LoginBean();
  validateForm: FormGroup;
  isVisible = false;
  showActive = 1;
  config = Config;
  androidDw = "app/android.png";
  iosDw = "app/ios.png";
  reminder_err = false;
  reminder_message;
  currentModal: NzModalSubject;
  getUserEmail: any;
  getUserCode: any;
  showWz = false;
  isSpinning = false;
  constructor(
    private fb: FormBuilder,
    private router: Router,
    private httpServcie: HttpServcie,
    private globalVars: GlobalVars,
    private _notification: NzNotificationService,
    private nsub: NzModalSubject,
    private modalService: NzModalService
  ) {
    // setInterval(function () {
    //   debugger;
    //   console.log("你想干嘛?");
    // }, 300)
    if ((window.location.href + "").indexOf("wzscjg.hyjckj.net") >= 0) {
      this.showWz = true;
    }
  }

  ngOnInit() {
    this.validateForm = this.fb.group({
      userName: [null, [Validators.required]],
      password: [null, [Validators.required]]
    });
    window.localStorage.setItem("funcPermissions", "");
  }

  _submitForm() {
    this.isSpinning = true;
    this.httpServcie.login(this.loginBean).subscribe(data => {
      this.isSpinning = false;
      if (data.status !== 200) {
        this.reminder_err = true;
        this.reminder_message = data.message;
        setTimeout(() => {
          this.reminder_err = false;
        }, 2000);
      } else {
        // 放这里是因为全局变量刷新浏览器后全局变量就没啦
        window.localStorage.setItem("user", JSON.stringify(data.data["sysUser"])
        );
        window.localStorage.setItem("token", data.data["token"]);

        this.httpServcie.findFuncSID(this.loginBean).subscribe(data => {
          window.localStorage.setItem("funcPermissions", data.data);
          this.router.navigate(["/home/main"]);
        });
      }
    }, err => {
      this.isSpinning = false;
    });
  }

  // 安全监管
  safety() {
    this.showActive = 1;
  }
  // 维保单位
  maintenance() {
    this.showActive = 2;
  }
  // 使用单位
  useCompany() {
    this.showActive = 3;
  }
  // 检验单位
  checkCompany() {
    this.showActive = 4;
  }

  // 注册页面
  register() {
    this.router.navigate(["/register"]);
  }

  /**
   * 无纸化维保申请页面，该功能用于使用单位，维保单位提交使用系统申请
   */
  paperlessMaintence() {
    this.router.navigate(["/paperless-maintenance-apply"]);
  }

  forget(titleforget, contentforget, footerforget) {
    this.currentModal = this.modalService.open({
      title: titleforget,
      content: contentforget,
      footer: footerforget,
      maskClosable: false,
      width: 600,
      style: {
        top: "20%"
      },

      onOk() {
        console.log("Click ok");
      }
    });
  }

  getMyPassWord() {
    let param = {
      userCode: this.getUserCode,
      email: this.getUserEmail
    };
    this.httpServcie.forgetPwd(param).subscribe(data => {
      if (data.status == 200) {
        this._notification.create(
          "success",
          "找回成功",
          "请前往邮箱查看用户密码"
        );
        this.currentModal.destroy();
      } else {
        this._notification.create(
          "error",
          "找回失败",
          "请确认您的邮箱否则正确！"
        );
      }
    });
  }

  showModal = () => {
    this.isVisible = true;
  };

  handleCancel = e => {
    this.isVisible = false;
  };
}
